﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Schedule
{
    class CourseDataManage
    {
        private List<Course> courses = Course.courses;
        private LinkDB link = new LinkDB();

        public void dataInit()
        {
            link.getCourses();
        }

        public bool deleteCourse(int weekday,int chooseLesson)
        {
            if (!courses.Exists(c => c.Weekday == weekday && c.BeginLesson <= chooseLesson && (c.LastLesson + c.BeginLesson) > chooseLesson))
                return false;//无课程
            string cID = courses.Find(c => c.Weekday == weekday && c.BeginLesson <= chooseLesson && (c.LastLesson + c.BeginLesson) > chooseLesson).CourseID.ToString();
            link.deleteCourse(cID);
            new DDLManage().deleteBycourse(cID);//删除课程对应的ddl
            courses.RemoveAll(c => c.Weekday == weekday && c.BeginLesson <= chooseLesson && (c.LastLesson + c.BeginLesson) > chooseLesson);
            return true;
        }

        public void getCourseInfo(int weekday,int chooseLesson, out string courseID, out string courseName)
        {
            courseID = "";
            courseName = "";
            var query = from c in courses
                        where c.Weekday == weekday && c.BeginLesson <= chooseLesson && (c.LastLesson + c.BeginLesson) > chooseLesson
                        select c;
            try
            {
                courseID = ((Course)query.ElementAt(0)).CourseID.ToString();
                courseName = ((Course)query.ElementAt(0)).Name;
            }
            catch (Exception) { return; }
        }
        
        public void addCourse(long courseID,string courseName,int weekday,int beginLesson,int lastLesson)
        {
            Course newCourse = new Course(courseID, courseName, weekday, beginLesson, lastLesson);
            courses.Add(newCourse);
            link.addCourse(newCourse);
        }
    }
}
